package com.sdy.web;

import com.alibaba.fastjson.JSONObject;
import com.sdy.po.Company;
import com.sdy.po.Parm;
import com.sdy.po.SerialPO;
import com.sdy.po.User;
import com.sdy.service.SerialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/serial")
public class SerialController {

    @Autowired
    private SerialService serialService;

    /**
     * 串口信息模块
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("")
    public String setUpSerials(HttpSession session, Model model){

        Company contactCompany = (Company) session.getAttribute("contactCompany");

        //串口信息
        List<SerialPO> serialList = serialService.findByComandId(contactCompany.getId());

        model.addAttribute("Serials",serialList);

        return "pages/setUpSerials";
    }

    /**
     * 修改串口
     * @param session
     * @param serialNumber
     * @param baudRate
     * @param checkoutBit
     * @param dataBit
     * @param stopBit
     * @param parm
     * @param parmCN
     * @param isHandled
     * @return
     */
    @ResponseBody
    @RequestMapping("update")
    public JSONObject update(
            HttpSession session,
            @RequestParam(value = "serialNumber") String serialNumber,     // 串口号
            @RequestParam(value = "baudRate")Integer baudRate,       // 波特率
            @RequestParam(value = "checkoutBit")Integer checkoutBit,     // 校验位
            @RequestParam(value = "dataBit")Integer dataBit,         // 数据位
            @RequestParam(value = "stopBit")Integer stopBit,         // 停止位
            @RequestParam(value = "parm")String parm,         //监控的数据名
            @RequestParam(value = "parmCN")String parmCN,       //中文名
            @RequestParam(value = "ishandled")Boolean isHandled){

        Company contactCompany = (Company) session.getAttribute("contactCompany");

        Boolean result = serialService.update(new SerialPO(contactCompany.getId(),serialNumber,baudRate,checkoutBit,dataBit,stopBit,1,isHandled));

        System.out.println(result);

        return null;
    }

    /**
     * 添加串口
     * @return
     */
    @ResponseBody
    @RequestMapping("insert")
    public JSONObject insert(
            HttpSession session,
            @RequestParam(value = "serialNumber") String serialNumber,     // 串口号
            @RequestParam(value = "baudRate")Integer baudRate,       // 波特率
            @RequestParam(value = "checkoutBit")Integer checkoutBit,     // 校验位
            @RequestParam(value = "dataBit")Integer dataBit,         // 数据位
            @RequestParam(value = "stopBit")Integer stopBit,         // 停止位
            @RequestParam(value = "parm")String parm,         //监控的数据名
            @RequestParam(value = "parmCN")String parmCN,       //中文名
            @RequestParam(value = "ishandled")Boolean isHandled){

        Company contactCompany = (Company) session.getAttribute("contactCompany");

        Boolean result = serialService.insert(new SerialPO(contactCompany.getId(),serialNumber,baudRate,checkoutBit,dataBit,stopBit,1,isHandled));

        System.out.println(result);
        return null;
    }
}
